from PyQt4 import QtGui, QtCore
from PyQt4.QtCore import Qt
from .tart_header import TartHeader
from .tart_bar import TartBar
from ..data.wlist import WList
from operator import attrgetter

class TartTable(QtGui.QWidget):
	new_selection = QtCore.pyqtSignal(object)
	job_box_clicked = QtCore.pyqtSignal(object, object)

	def __init__(self, *args, **kw):
		QtGui.QWidget.__init__(self, *args, **kw)
#
#		set the header for the table,
#		move it into place and catch the sort signal
#
		self.header = TartHeader(self);
		self.header.move(26,0)
		self.header.sortme.connect(self.sort)
#
#		rows holds the list of TartBar elements that
#		make up the rows in the table
#
		self.make_rows()
#
#		keyboard shortcuts
#
		self.make_shortcuts()
#
#		we use a windowed list to page through the data
#
		self.wlist = WList()
		self.wlist.changed.connect(self.refresh)

	def make_rows(self):
		self.rows = []
#
#		create 11 bars. it seems a strange number,
#		but that's how many fit.
#
#		I may reduce it to 10 and make room for some
#		controls underneat at some stage
#
		yoff = 25
		for i in range(11):
#
#			make a row
#
			row = TartBar(self, index = i)
#
#			add it to the list
#
			self.rows.append(row)
#
#			move it into position
#
			row.move(0, yoff)
			yoff += 52
#
#			catch clicks
#
			row.clicked.connect(self.on_click)
			row.job_box_clicked.connect(self.job_box_clicked)

#
#	kudos to rudistoned for the code
#	I like the trick of using shorthand variables for classes
#
	def make_shortcuts(self):
		Seq = QtGui.QKeySequence
		Shorty = QtGui.QShortcut
		Shorty(Seq(Qt.Key_Down), self, self.down_key)
		Shorty(Seq(Qt.Key_Up), self, self.up_key)
		Shorty(Seq(Qt.Key_PageUp), self, self.pgup_key)
		Shorty(Seq(Qt.Key_PageDown), self, self.pgdown_key)
		Shorty(Seq(Qt.Key_Home), self, self.home_key)
		Shorty(Seq(Qt.Key_End), self, self.end_key)

	def home_key(self):
		self.wlist.cursor = 0
		self.clear_sel()	# clear any selection
		self.wlist.current.selected = True
		self.refresh()
		self.new_selection.emit(self.wlist.current)

	def end_key(self):
		self.wlist.cursor = 	len(self.wlist.list) - 1
		self.clear_sel()	# clear any selection
		self.wlist.current.selected = True
		self.refresh()
		self.new_selection.emit(self.wlist.current)

	def pgup_key(self):
		self.wlist.back()
		self.refresh()
		self.new_selection.emit(self.wlist.current)

	def pgup_key(self):
		self.wlist.back()
		self.refresh()
		self.new_selection.emit(self.wlist.current)

	def pgdown_key(self):
		self.wlist.page()
		self.refresh()
		self.new_selection.emit(self.wlist.current)

	def up_key(self):
		self.wlist.current.selected = False
		self.wlist.cursor -= 1
		self.wlist.current.selected = True
		self.refresh()
		self.new_selection.emit(self.wlist.current)

	def down_key(self):
		#self.debug_trace()
		self.wlist.current.selected = False
		self.wlist.cursor += 1
		self.wlist.current.selected = True
		self.refresh()
		self.new_selection.emit(self.wlist.current)

	def debug_trace(self):
		'''Set a tracepoint in the Python debugger that works with Qt'''
		from PyQt4.QtCore import pyqtRemoveInputHook
		from pdb import set_trace
		pyqtRemoveInputHook()
		set_trace()


#
#	sort the list based on column name and direction
#
	def sort(self, col_name, rev):
		f = None
#
#		we can use attrgetter as the sort key
#		we need to convert the QString to a str though
#
		f = attrgetter(str(col_name))
#
#		do the sort. we need the currently selected
#		girl to stay current or odd things happen
#		so let's make sure we know which one it is
#
		curr = self.wlist.current
#
#		do the sort
#
		self.wlist.sort(f, rev)
#
#		set the cursor to the (probably moved) current girl
#
		self.wlist.cursor = curr
		self.refresh()

#
#	if we get clicked
#
	def on_click(self, bar):
		if bar.girl == None:
			return
#
#		we need to know if amy modifier keys were down
#
		mods = QtGui.QApplication.keyboardModifiers()
#
#		particularly shift
#
		if mods & QtCore.Qt.ShiftModifier:
			pass
		else:
			self.clear_sel()	# clear any selection
			self.wlist.cursor = bar.girl
			self.new_selection.emit(self.wlist.current)
		bar.girl.selected = True	# select the girl on the bar
		self.refresh()			# refresh the display


	def clear_sel(self):
		for girl in self.wlist.list:
			girl.selected = False
		self.refresh()			# refresh the display

	def set_girls(self, list):
		self.wlist = WList()
		self.wlist.list = list

		sel = True
		for girl in list:
			girl.selected = sel
			sel = False
		self.refresh()
		self.new_selection.emit(self.wlist.current)

	def refresh(self):
		i = 0
		for i, g in enumerate(self.wlist.window):
			if i >= len(self.rows):
				break
			row = self.rows[i]
			row.set_girl(g, i)
			row.highlight(g.selected)
		for i in range(i+1, 11):
			self.clear_row(i)
			

	def clear_row(self, index):
		row = self.rows[index]
		row.set_girl(None, 0)

	def highlight(self, index):
		self.rows[index].highlight()

if __name__ == "__main__":
	import sys
	from lib.mocks.girly import test_girls

	app = QtGui.QApplication(sys.argv)

	pic = QtGui.QLabel()
	pic.setGeometry(0, 0, 1024, 768)
	path = "resources/wood.jpg"
	pic.setPixmap(QtGui.QPixmap(path))

	screen = TartTable(pic)
	screen.set_girls(test_girls[:])

	pic.show()
	sys.exit(app.exec_())





